<?php


namespace App\Services\Result;

use App\Interf\Resultable;
use Monolog\Logger;

use ParamConfig;
use App\Models\LogHelper;

class BoneHealthDuringPregnancyTestingResult extends BaseResult implements Resultable
{
    private $answers;

    private $summaryObj;

    public function __construct($answersStr = null)
    {

        $this->answers = $this->convertAnswer($answersStr);
        $this->summaryObj = $this->configAttrObj(1, '测评结果');


    }


    public function result()
    {

        $attrs = [];
        array_push($attrs, $this->getSummaryAttr());
        $data['attrs'] = $attrs;
        $data['chatparam'] = $this->getChatparam();
        //图类型常规图
        $data['chatType'] = [];
        return $data;
    }


    protected function getSummaryAttr()
    {
        $note = '';
        $extra = '';
        $answer17= $this->getDefaultAnswerObjByIndex(17);

        if ($answer17['weight'] == 0) {
            $temp = ParamConfig::getValue('bone.health.during.pregnancy.testing.part.two.one');
            $note .= $temp;
        } elseif ($answer17['weight'] >0 && $answer17['weight'] <= 21) {
            $temp = ParamConfig::getValue('bone.health.during.pregnancy.testing.part.two.two');
            $note .= $temp;
        } elseif ($answer17['weight'] > 21 &&  $answer17['weight'] <= 27) {
            $temp = ParamConfig::getValue('bone.health.during.pregnancy.testing.part.two.three');
            $note .= $temp;
        } elseif ($answer17['weight'] >27 &&  $answer17['weight'] <= 62) {
            $temp = ParamConfig::getValue('bone.health.during.pregnancy.testing.part.two.four');
            $note .= $temp;
        }

        $obj = $this->summaryObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note, $extra);

        LogHelper::log($obj);
        return $obj;
    }

    protected function getChatparam()
    {
        $data = [];

        return $data;
    }

    protected function compactAttrObj($id, $title, $note, $extra = '')
    {
        return [
            'attr_id' => $id,
            'title' => $title,
            'note' => $note,
            'extra' => $extra
        ];
    }

    protected function getDefaultAnswerObjByIndex($index)
    {
        $obj = [];
        foreach ($this->answers as $answer) {
            if ($index == $answer['index']) {
                $obj = $answer;
                break;
            }
        }
        return $obj;
    }

    /**将answer转换为数组
     * @param $answerStr
     * @return array
     */
    protected function convertAnswer($answerStr)
    {
        LogHelper::log('FINAL ANSWER STR============>');
        LogHelper::log($answerStr);
        $answers = [];
        $answerArr = explode('##', $answerStr);
        if (count($answerArr) > 0) {
            foreach ($answerArr as $answer) {
                $singleArr = explode(':', $answer);
                if (count($singleArr) > 0) {
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers, $obj);
                }
            }
        }
        return $answers;
    }

}